//
//  NUserInfoViewController.m
//  News
//
//  Created by zhangtao on 2018/10/15.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NUserInfoViewController.h"
#import "NMyInfoHeadView.h"
#import "NUpDateInfoViewController.h"
#import "NUpLoadChange.h"
@interface NUserInfoViewController ()<NMyInfoHeadViewDelegate,LCActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) NMyInfoHeadView *heardView;

@end

@implementation NUserInfoViewController
{
    NSArray *titles;
}

-(instancetype)initWithStyle:(UITableViewStyle)style{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    titles = @[@"昵称",@"性别",@"个性签名"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return titles.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 140;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.heardView = [[NMyInfoHeadView alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, 140)];
    self.heardView.delegate = self;
    return self.heardView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    cell.textLabel.text = titles[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = BASE_FONT;
    cell.detailTextLabel.font = MIN_FONT;
    cell.detailTextLabel.textColor = [UIColor darkGrayColor];
    cell.detailTextLabel.text = titles[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NUpDateInfoViewController *update = [[NUpDateInfoViewController alloc]init];
    update.cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [self.navigationController pushViewController:update animated:YES];
}

#pragma mark -- NMyInfoHeadViewDelegate
- (void)upLoadHeardImageViewBtnClickWithSelf:(NMyInfoHeadView *)view
{
    LCActionSheet *actionSheet;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        actionSheet  = [[LCActionSheet alloc] initWithTitle:nil
                                               buttonTitles:@[@"拍一张",@"从相册选择",]
                                             redButtonIndex:-1
                                                   delegate:self];
    }else {
        actionSheet = [[LCActionSheet alloc] initWithTitle:nil
                                              buttonTitles:@[@"从相册选择"]
                                            redButtonIndex:-1
                                                  delegate:self];
    }
    [actionSheet show];
}

#pragma mark - LCActionSheet 代理方法
- (void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSUInteger sourceType = 0;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
            case 0:
                // 相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                // 相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
            default:
                break;
        }
    } else {
        switch (buttonIndex) {
            case 0:
                // 相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 1:
                return;
            default:
                break;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = sourceType;
    [self presentViewController:imagePickerController animated:YES completion:^{}];
}

#pragma mark --- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    [self uploadPhoto:[info objectForKey:UIImagePickerControllerOriginalImage]];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // bug fixes: UIIMagePickerController使用中偷换StatusBar颜色的问题
    if ([navigationController isKindOfClass:[UIImagePickerController class]] &&
        ((UIImagePickerController *)navigationController).sourceType ==     UIImagePickerControllerSourceTypePhotoLibrary) {
        navigationController.navigationBar.translucent = NO;
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }
}

- (void)uploadPhoto:(UIImage *)img
{
    NSDictionary *param;
    NSString *url;
    param = [NUpLoadChange uploadImage:img];
    url = HTTP_POST_Community_UploadHeadPic;
    [[NHTTPManage shareInstance] uploadImageWithUrl:url andParam:param success:^(id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject[@"Data"];
        [[NHTTPManage shareInstance] afPOST:HTTP_POST_UpdateHeadpic andParam:@{@"token":TOKEN,@"cuid":CUID,@"pic":dic[@"FileName"]} success:^(id  _Nonnull responseObject) {
            [self.heardView.headImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImageUrl,dic[@"FileName"]]] placeholderImage:[UIImage imageNamed:@"img"]];
        } failture:^(id  _Nonnull error) {
            NSLog(@"%@",error);
        }];
    } fail:^(id  _Nonnull error) {
        NSLog(@"%@",error);
    }];
   
}


@end
